package com.xiaoyang.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @AUTHOR XiaoYang
 * @DATE 2023/7/3-15:31
 * @DESCRIPTION TODO whenComplete计算完成回调
 */
public class CompletableFutureDemo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池（实际中不这么做）
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        System.out.println("main begin...");

        //CompletableFuture创建异步对象
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("now thread :" + Thread.currentThread().getName());
            int value = 1024 / 0;
            System.out.println("value :" + value);
            return value;
        }, executorService);
        //                 两个参数：
        //                  1、上一个任务的返回结果
        //                  2、异常
        future.whenComplete((rs, exception) -> {
            System.out.println("whenComplete :" + rs);
            System.out.println("exception :" + exception);
        });

        System.out.println("main over...");
    }
}
